package com.centling.smartSealForPhone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.adapter.AuditorSealManagerAdapter;
import com.centling.smartSealForPhone.bean.AuditorSealBean;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;
import com.centling.smartSealForPhone.widget.AutoRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 *
 * @author jessie
 * @date 2016/8/16
 */
public class AuditorSealHistoryActivity extends TitleBarActivity {

    private AutoRecyclerView auditorRecyclerView;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private int pageNo = 0;
    private List<AuditorSealBean> auditorSealListBean = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auditor_seal_manager);
        setTitleBarText("历史记录");
        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_auditor_seal);
        auditorRecyclerView = (AutoRecyclerView) findViewById(R.id.rv_auditor_seal);
        setTitleBarRight(R.drawable.ic_history_search);
        setTitleBarRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, AuditorSearchActivity.class));
            }
        });
        AuditorSealManagerAdapter adapter = new AuditorSealManagerAdapter(mContext,
                auditorSealListBean);
        auditorRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        auditorRecyclerView.setAdapter(adapter);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                sendRequest(true);
            }
        });

        auditorRecyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                sendRequest(false);
            }
        });
    }

    private void sendRequest(final boolean isRefresh) {
        if (isRefresh) {
            pageNo = 0;
        }
        getEnqueue(UrlConstants.GET_SEAL_LIST + pageNo, new OkHttpUtil.NetCallBack() {
            @Override
            public void onSucceed(String json) {
                if (isRefresh) {
                    auditorSealListBean.clear();
                    auditorRecyclerView.setLoadDataListener(new AutoRecyclerView.LoadDataListener() {
                        @Override
                        public void onLoadMore() {
                            sendRequest(false);
                        }
                    });
                }
                JSONArray data = null;
                try {
                    data = new JSONObject(json).getJSONArray("rows");
                    List<AuditorSealBean> auditorSealListBeanTemp;

                    auditorSealListBeanTemp = new Gson().fromJson(data.toString(), new TypeToken<ArrayList<AuditorSealBean>>() {
                    }.getType());
                    auditorSealListBean.addAll(auditorSealListBeanTemp);
                    auditorRecyclerView.getAdapter().notifyDataSetChanged();
                    ptrClassicFrameLayout.refreshComplete();
                    pageNo++;
                    if (auditorSealListBeanTemp.size() < 20) {
                        auditorRecyclerView.loadMoreComplete(true);
                    } else {
                        auditorRecyclerView.loadMoreComplete(false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(String reason) {
                ptrClassicFrameLayout.refreshComplete();
                auditorRecyclerView.loadMoreComplete(false);
                ShowToast.shortTime(reason);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        auditorRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh();
            }
        });
    }
}
